package com.gzw.dms.activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by Wangzhiqiang on 2015/12/30.
 */
public class ShippingOrderDetailActivity extends BaseActivity {
    private TextView tv_shiping_order_num, tv_custom_name, tv_custom_contact,
            tv_custom_tel, tv_receipt_name, tv_receipt_tel, tv_goods_name, tv_goods_num, tv_goods_weight,
            tv_goods_vol, tv_shipping_order_state, tv_is_recept, tv_remarks,tv_send_station,ic_receipt
            ,tv_end_mechanism,tv_shiping_order_goid,tv_send_mechanism,ic_send_time,ic_end_address,ic_shiping_yueid;
    private Button btn_edit;
    private Intent mIntent;
    private ShippingOrderInfo shippingOrder;
    private ProgressDialog mProgressDialog;
    private Context mContext;
    private Toolbar toolbar;
    //0、1为接单，2已接单，3电子围栏签收，4实际签收,5分配订单
    private  String state;
    private  String fpstste="0";
    private List<String> ls_map;
    private LinearLayout layout_receipt;
    private String driver_id = "";
    private String equipment_number = "";
    private String username = "";
    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_shippingorderdetail);
    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tv_shiping_order_num = (TextView) findViewById(R.id.ic_shiping_order_num).findViewById(R.id.tv_name);
        tv_shiping_order_goid = (TextView) findViewById(R.id.ic_shiping_order_goid).findViewById(R.id.tv_name);
        tv_send_station = (TextView) findViewById(R.id.ic_send_station).findViewById(R.id.tv_name);


        ic_end_address = (TextView) findViewById(R.id.ic_end_address).findViewById(R.id.tv_name);
        ic_send_time = (TextView) findViewById(R.id.ic_send_time).findViewById(R.id.tv_name);
        ic_shiping_yueid = (TextView) findViewById(R.id.ic_shiping_yueid).findViewById(R.id.tv_name);
        ic_receipt = (TextView) findViewById(R.id.ic_receipt).findViewById(R.id.tv_name);

//        tv_end_address = (TextView) findViewById(R.id.ic_end_address).findViewById(R.id.tv_name);
        tv_custom_name = (TextView) findViewById(R.id.ic_custom_name).findViewById(R.id.tv_name);
        tv_custom_contact = (TextView) findViewById(R.id.ic_custom_contact).findViewById(R.id.tv_name);
        tv_custom_tel = (TextView) findViewById(R.id.ic_custom_tel).findViewById(R.id.tv_name);
        tv_receipt_name = (TextView) findViewById(R.id.ic_receipt_name).findViewById(R.id.tv_name);
        tv_receipt_tel = (TextView) findViewById(R.id.ic_receipt_tel).findViewById(R.id.tv_name);
        tv_end_mechanism = (TextView) findViewById(R.id.ic_end_mechanism).findViewById(R.id.tv_name);
        tv_goods_name = (TextView) findViewById(R.id.ic_goods).findViewById(R.id.tv_left);
        tv_goods_num = (TextView) findViewById(R.id.ic_goods).findViewById(R.id.tv_right);
        tv_goods_weight = (TextView) findViewById(R.id.ic_goodsWV).findViewById(R.id.tv_left);
        tv_goods_vol = (TextView) findViewById(R.id.ic_goodsWV).findViewById(R.id.tv_right);
        tv_shipping_order_state = (TextView) findViewById(R.id.ic_shipping_order_state).findViewById(R.id.tv_left);
        tv_is_recept = (TextView) findViewById(R.id.ic_shipping_order_state).findViewById(R.id.tv_right);
        tv_remarks = (TextView) findViewById(R.id.ic_remarks).findViewById(R.id.tv_name);
        tv_send_mechanism= (TextView) findViewById(R.id.ic_send_mechanism).findViewById(R.id.tv_name);
        btn_edit = (Button) findViewById(R.id.btn_edit);
        layout_receipt = (LinearLayout) findViewById(R.id.layout_receipt);

    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("运单详情");
        mIntent = getIntent();
        ls_map = new ArrayList<>();
        setSupportActionBar(toolbar);

        shippingOrder = (ShippingOrderInfo) mIntent.getExtras().get(
                "shippingOrder");
        fpstste=(String)mIntent.getExtras().get("fpstste");
        driver_id = (String)mIntent.getExtras().get("driver_id");
        if (fpstste.equals("8")){
            equipment_number = (String)mIntent.getExtras().get("equipment_number");
            username = (String)mIntent.getExtras().get("username");
        }
        setData(shippingOrder);
    }

    @Override
    protected void initEvents() {
        btn_edit.setOnClickListener(new mOnClick());
        toolbar.setNavigationOnClickListener(new mOnClick());

    }

    private void setData(ShippingOrderInfo shippingOrderInfo) {
        tv_shiping_order_num.setText("货运编号:" + "    " + shippingOrderInfo.getShiping_order_num());
        tv_shiping_order_goid.setText("出货订单号:" + "    " + shippingOrderInfo.getShiping_order_goid());
        tv_send_station.setText("起  运  地:" + "    " + shippingOrderInfo.getSend_station());

        ic_send_time.setText("起运时间:" + "    " + shippingOrderInfo.getSend_time());
        ic_end_address.setText("终到位置:" + "    " + shippingOrderInfo.getEnd_address());
        ic_shiping_yueid.setText("月结编号:" + "    " + shippingOrderInfo.getShiping_yueid());
        ic_receipt.setText("收货客户地址:" + "    " + shippingOrderInfo.getEnd_mechanism());

//        tv_end_address.setText("终到位置:" + "    " + shippingOrderInfo.getEnd_address());
        tv_custom_name.setText("发货客户名称:" + "    " + shippingOrderInfo.getCustom_name());
        tv_custom_contact.setText("发货联系人:" + "    " + shippingOrderInfo.getCustom_contact());
        tv_custom_tel.setText("发货客户电话:" + "    " + shippingOrderInfo.getCustom_tel());
        tv_receipt_name.setText("收货客户名称:" + "    " + shippingOrderInfo.getReceipt());
        tv_receipt_tel.setText("收货客户电话:" + "    " + shippingOrderInfo.getReceipt_tel());
        tv_end_mechanism.setText("收货联系人:" + "    " + shippingOrderInfo.getReceipt_name());

        tv_goods_name.setText("货物名称:" + "    " + shippingOrderInfo.getGoods_name());
        tv_goods_num.setText("总件数:" + "    " + shippingOrderInfo.getGoods_num());
        tv_goods_weight.setText("总重量:" + "    " + shippingOrderInfo.getGoods_weight() + "kg");
        tv_goods_vol.setText("总体积:" + "    " + shippingOrderInfo.getGoods_vol() + "m³");
        tv_send_mechanism.setText("受理机构:" + "    " + shippingOrderInfo.getSend_mechanism());
        tv_remarks.setText("发货备注:" + "    " + shippingOrderInfo.getRemarks());

        //订单状态
        state = String.valueOf(shippingOrderInfo.getShipping_order_state());

        if(fpstste.equals("5")){
            tv_shipping_order_state.setText("运单状态:未出库");
            btn_edit.setText("分    配");
            btn_edit.setVisibility(View.VISIBLE);
        }else if(fpstste.equals("4")){
            if (state.equals("0")) {
                tv_shipping_order_state.setText("运单状态:未出库");
                btn_edit.setText("接    单");
                btn_edit.setVisibility(View.GONE);
            } else if (state.equals("1")) {
                tv_shipping_order_state.setText("运单状态:分配订单");
                btn_edit.setText("接    单");
                btn_edit.setVisibility(View.GONE);
            } else if (state.equals("2")) {
                tv_shipping_order_state.setText("运单状态:司机接单");
                btn_edit.setText("签    收");
                btn_edit.setVisibility(View.GONE);
            } else if (state.equals("3")) {
                tv_shipping_order_state.setText("运单状态:电子围栏签收");
                btn_edit.setVisibility(View.GONE);
                btn_edit.setText("签    收");
            } else if (state.equals("4")) {
                tv_shipping_order_state.setText("运单状态:实际签收");
                btn_edit.setVisibility(View.GONE);
            }else if (state.equals("5")) {
                tv_shipping_order_state.setText("运单状态:已返回");
                btn_edit.setVisibility(View.GONE);
            }else if (state.equals("6")) {
                tv_shipping_order_state.setText("运单状态:已收到");
                btn_edit.setVisibility(View.GONE);
            }
        } else if(fpstste.equals("6")){
            if (state.equals("0")) {
                tv_shipping_order_state.setText("运单状态: 未出库");
                btn_edit.setText("上    报");
                btn_edit.setVisibility(View.VISIBLE);
            } else if (state.equals("1")) {
                tv_shipping_order_state.setText("运单状态:分配订单");
                btn_edit.setText("上    报");
                btn_edit.setVisibility(View.VISIBLE);
            } else if (state.equals("2")) {
                tv_shipping_order_state.setText("运单状态:司机接单");
                btn_edit.setText("上    报");
                btn_edit.setVisibility(View.VISIBLE);
            } else if (state.equals("3")) {
                tv_shipping_order_state.setText("运单状态:电子围栏签收");
                btn_edit.setVisibility(View.VISIBLE);
                btn_edit.setText("上    报");
            } else if (state.equals("4")) {
                tv_shipping_order_state.setText("运单状态:实际签收");
                btn_edit.setVisibility(View.VISIBLE);
                btn_edit.setText("上    报");

            }else if (state.equals("5")) {
                tv_shipping_order_state.setText("运单状态:已返回");
                btn_edit.setVisibility(View.VISIBLE);
                btn_edit.setText("上    报");

            }else if (state.equals("6")) {
                tv_shipping_order_state.setText("运单状态:已收到");
                btn_edit.setVisibility(View.VISIBLE);
                btn_edit.setText("上    报");

            }
        }else if(fpstste.equals("7")){
           layout_receipt.setVisibility(View.GONE);
            btn_edit.setVisibility(View.GONE);

        } else if(fpstste.equals("8")){
            if (state.equals("0")) {
                tv_shipping_order_state.setText("运单状态: 未出库");
                btn_edit.setText("删    除");
                btn_edit.setVisibility(View.VISIBLE);
            } else if (state.equals("1")) {
                tv_shipping_order_state.setText("运单状态:分配订单");
                btn_edit.setText("删    除");
                btn_edit.setVisibility(View.VISIBLE);
            } else if (state.equals("2")) {
                tv_shipping_order_state.setText("运单状态:司机接单");
                btn_edit.setText("删    除");
                btn_edit.setVisibility(View.VISIBLE);
            } else if (state.equals("3")) {
                tv_shipping_order_state.setText("运单状态:电子围栏签收");
                btn_edit.setVisibility(View.VISIBLE);
                btn_edit.setText("删    除");
            } else if (state.equals("4")) {
                tv_shipping_order_state.setText("运单状态:实际签收");
                btn_edit.setVisibility(View.VISIBLE);
                btn_edit.setText("删    除");

            }else if (state.equals("5")) {
                tv_shipping_order_state.setText("运单状态:已返回");
                btn_edit.setVisibility(View.VISIBLE);
                btn_edit.setText("删    除");

            }else if (state.equals("6")) {
                tv_shipping_order_state.setText("运单状态:已收到");
                btn_edit.setVisibility(View.VISIBLE);
                btn_edit.setText("删    除");

            }

        } else {

            if (state.equals("0")) {
                tv_shipping_order_state.setText("运单状态:    未出库");
                btn_edit.setText("接    单");
                btn_edit.setVisibility(View.VISIBLE);
            } else if (state.equals("1")) {
                tv_shipping_order_state.setText("运单状态:分配订单");
                btn_edit.setText("接    单");
                btn_edit.setVisibility(View.VISIBLE);
            } else if (state.equals("2")) {
                tv_shipping_order_state.setText("运单状态:司机接单");
                btn_edit.setText("签    收");
                btn_edit.setVisibility(View.VISIBLE);
            } else if (state.equals("3")) {
                tv_shipping_order_state.setText("运单状态:电子围栏签收");
                btn_edit.setVisibility(View.VISIBLE);
                btn_edit.setText("签    收");
            } else if (state.equals("4")) {
                tv_shipping_order_state.setText("运单状态:实际签收");
                btn_edit.setVisibility(View.GONE);
            }
        }
        tv_is_recept.setText("回单份数:" + "    " + shippingOrderInfo.getIs_recept());
    }

    /**
     * Created by Wangzhiqiang on 2015/11/19.
     * button的点击方法
     */
    class mOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.btn_edit:
                    if (fpstste.equals("5")) {
                        ls_map.add(shippingOrder.getShiping_order_id());
                        FPOrderTask fpOrderTask = new FPOrderTask();
                        fpOrderTask.execute();
//                        Intent mIntetn = new Intent();
//                        mIntetn.setClass(mContext, SelectDriverActivity.class);
//                        mIntetn.putStringArrayListExtra("ls_map", (ArrayList<String>) ls_map);
//                        startActivityForResult(mIntetn, 1);
                    }else if (fpstste.equals("8")){
                        ls_map.add(shippingOrder.getShiping_order_id());
                        DeleteOrderTask deleteOrderTask = new DeleteOrderTask();
                        deleteOrderTask.execute();

                    }else {
                        if (state.equals("0") || state.equals("1")) {
                            String driver_id = MyApplication.getUserInfo().getDriver_id();
                            String shiping_order_id = shippingOrder.getShiping_order_id();
                            String order_num = shippingOrder.getShiping_order_num();
                            String driver_name = MyApplication.getUserInfo().getUsername();
                            ReceiveOrderTask receiveOrderTask = new ReceiveOrderTask();
                            receiveOrderTask.execute(driver_id, shiping_order_id, order_num,driver_name);

                        } else if (state.equals("2")) {
                            String is_abnormal = shippingOrder.getIs_abnormal();
                            if (is_abnormal.equals("1") || is_abnormal.equals("2")) {
                                T.showShort(mContext, "该运单已异常上报，请联系项目部先处理");

                            }else {
                                Integer waybill= shippingOrder.getWaybill_state();
                                if (waybill==1 || waybill==2){
                                    T.showShort(mContext,"该运单已进行调整，请等待处理...");
                                }else {
                                    Intent mIntetn = new Intent();
                                    Bundle bundle = new Bundle();
                                    mIntetn.setClass(mContext, ScanActivity.class);
                                    bundle.putSerializable("shippingOrder", shippingOrder);
                                    mIntetn.putExtras(bundle);
                                    startActivityForResult(mIntetn, Integer.parseInt(state));
                                }

                            }

                        } else if (state.equals("3")) {

                            Integer waybill= shippingOrder.getWaybill_state();
                            if (waybill==1 || waybill==2){
                                T.showShort(mContext,"该运单已进行调整，请等待处理...");
                            }else {
                                Intent mIntetn = new Intent();
                                Bundle bundle = new Bundle();
                                mIntetn.setClass(mContext, ScanActivity.class);
                                bundle.putSerializable("shippingOrder", shippingOrder);
                                mIntetn.putExtras(bundle);
                                startActivityForResult(mIntetn, Integer.parseInt(state));
                            }

                        }

                    }
                    break;
                default:
                    finish();
                    break;
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //普通签收
        if (resultCode == 1) {
            Intent mIntent = new Intent();
            //设置返回数据
            setResult(1, mIntent);
            finish();
        }
    }

    public class ReceiveOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
            mProgressDialog.setCancelable(true);
        }

        protected String doInBackground(String... params) {
            List<Map<String ,String>> ls_map=new ArrayList<>();
            Map<String, String> map = new HashMap<>();
            map.put("driver_id", params[0]);
            map.put("shiping_order_id", params[1]);
            map.put("order_num",params[2]);
            map.put("driver_name", params[3]);
            ls_map.add(map);
            String json = JsonUtil.toJson(ls_map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appReceiveOrder"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            mProgressDialog.dismiss();
            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {

                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    T.showShort(mContext, successMsg);
                    Intent mIntent = new Intent();
                    //把返回数据存入Intent
                    mIntent.putExtra("result", 1);
                    //设置返回数据
                    setResult(1, mIntent);
                    finish();
                }
            }
        }

        @Override
        protected void onCancelled() {

        }
    }


    public class DeleteOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
            mProgressDialog.setCancelable(true);
        }

        protected String doInBackground(String... params) {
            String orders=ls_map.toString().substring(1,ls_map.toString().length()-1);
            String str1 = orders.replace(" ", "");
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("driverId",driver_id);
            map.put("orders",str1);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appDeleteShipOrderPage"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {

            mProgressDialog.dismiss();

            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    T.showShort(mContext, successMsg);

                    Intent mIntent = new Intent();
                    //把返回数据存入Intent
                    mIntent.putExtra("result", 1);
                    //设置返回数据
                    setResult(1, mIntent);
                    finish();
                }
            }

        }
    }


    public class FPOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
            mProgressDialog.setCancelable(true);
        }

        protected String doInBackground(String... params) {
            String orders=ls_map.toString().substring(1,ls_map.toString().length()-1);
            String str1 = orders.replace(" ", "");
            Map<String, String> map = new HashMap<>();
            map.put("driver_id", driver_id);
            map.put("orders", str1);
            map.put("equipmentNum",equipment_number);
            map.put("userName",username);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appFeiPeiShipperOrder"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {

            mProgressDialog.dismiss();

            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    T.showShort(mContext, successMsg);
                    Intent mIntent = new Intent();
                    //把返回数据存入Intent
                    mIntent.putExtra("result", 1);
                    //设置返回数据
                   setResult(1, mIntent);
                   finish();
                }
            }

        }
    }

}
